package com.tutu.category.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.tutu.category.service.CategoryService;
import com.tutu.commons.pojo.Category;
import com.tutu.commons.utils.R;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Program: b2c-cloud-store
 * @Description:
 * @Author: JK.TuTu
 * @Version: 1.0
 * @Create: 2022-12-15 11:14
 **/
@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 根据类别名称获取类别信息
     *
     * @param categoryName 类别名称
     * @return 返回结果实体对象
     */
    @ApiOperation("根据商品类别名称获取商品类别信息")
    @GetMapping("/promo/{categoryName}")
    R promo(@PathVariable("categoryName") String categoryName) {
        Category category = categoryService.getCategoryByName(categoryName);
        if (ObjectUtil.isNull(category)) {
            return R.fail("商城没有该类别!");
        }
        return R.ok(category);
    }


    /**
     * 获取所有商品分类信息
     *
     * @return 返回校验结果集
     */
    @ApiOperation("获取所有商品类别信息")
    @GetMapping("/getAll")
    public R getAll() {
        List<Category> categoryList = categoryService.getAll();
        if (CollUtil.isEmpty(categoryList)) {
            return R.fail("获取商品分类错误...");
        }
        return R.ok(categoryList);
    }


}
